<template>
  <!--   编辑弹出层 -->
  <div>
    <el-dialog
      :title="pagingname"
      :visible.sync="isDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :show-close="true"
      :before-close="handleDialogClose"
      destroy-on-close
      width="60%"
    >
      <div>
        <el-form
          :rules="rules"
          ref="ruleForm"
          :model="ruleForm"
          class="demo-ruleForm"
          label-width="120px"
        >
          <el-form-item label="数据源名称" class="item" prop="indexSetSourceId">
            <el-select
              v-model="ruleForm.indexSetSourceId"
              placeholder=""
              :disabled="isEditTable"
              class="Popup_inputwidth"
            >
              <el-option
                v-for="item in DropDown"
                :key="item.id"
                :label="item.dataSourceName"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="指标类型" class="item" prop="indexSetTypeId">
            <el-select
              v-model="ruleForm.indexSetTypeId"
              placeholder=""
              :disabled="isEditTable"
              class="Popup_inputwidth"
            >
              <el-option
                v-for="item in TheInterFace"
                :key="item.id"
                :label="item.indexTypeName"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item
            label="所属数据看版"
            class="item"
            prop="indexSetBelongBoardId"
          >
            <el-select
              v-model="ruleForm.indexSetBelongBoardId"
              placeholder=""
              :disabled="isEditTable"
              class="Popup_inputwidth"
            >
              <el-option
                v-for="item in NameLi"
                :key="item.id"
                :label="item.boardName"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="用户名" class="item" prop="createUser">
            <el-input
              v-model="ruleForm.createUser"
              placeholder="请输入用户名"
              size="small"
              class="Popup_inputwidth"
              clearable
            />
          </el-form-item>

          <el-form-item
            label="指标集计算sql"
            class="item"
            prop="indexSetCalcuSql"
          >
            <el-input
              v-model="ruleForm.indexSetCalcuSql"
              type="textarea"
              placeholder="请输入指标集计算使用的sql"
              size="small"
              class="Popup_inputwidth"
              clearable
            />
          </el-form-item>

          <el-form-item
            v-if="this.JudgeConditions"
            label="指标集编码"
            class="item"
          >
            <el-input
              v-model="ruleForm.indexSetCode"
              size="small"
              class="Popup_inputwidth"
              clearable
              disabled
            />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetField('ruleForm')">取 消</el-button>
        <el-button type="primary" @click="sureEdit('ruleForm')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  //   dataSource,
  //   NameList,
  //   TheInterface,
  IndexOfNew,
  UpdateInterface,
  //   DropDownQuery,
  //   isLogin,
} from "@/assets/js/api";
export default {
  props: {
    pagingname: {
      type: String,
      default: "",
    },
    isDialog: {
      type: Boolean,
      default: false,
    },
    JudgeConditions: {
      type: Boolean,
      default: false,
    },
    ruleForm: {
      type: Object,
      default: () => {},
    },
    DropDown: {
      type: Array,
      default: null,
    },
    TheInterFace: {
      type: Array,
      default: null,
    },
    NameLi: {
      type: Array,
      default: null,
    },
  },
  data() {
    return {
      isEditTable: false,
      // 校验规则
      rules: {
        indexSetSourceId: [
          { required: true, message: "请选择数据源名称", trigger: "change" },
        ],
        indexSetBelongBoardId: [
          { required: true, message: "请选择所属数据看版", trigger: "change" },
        ],
        indexSetTypeId: [
          { required: true, message: "请选择指标类型", trigger: "change" },
        ],
        indexSetCalcuSql: [
          {
            required: true,
            message: "请输入指标集计算使用的sql",
            trigger: "blur",
          },
          {
            required: true,
            message: "输入不能为空",
            pattern: "[^ \x22]+",
            trigger: "blur",
          },
        ],
        createUser: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            required: true,
            message: "输入不能为空",
            pattern: "[^ \x22]+",
            trigger: "blur",
          },
          // { required: true, message: '输入不能为空', pattern: /(^\s*)|(\s*$)/,trigger: "blur" }
        ],
      },
    };
  },
  methods: {
    // 点击x关闭
    handleDialogClose() {
      this.$emit("ResetData", true);
      this.$emit("isDialog", false);
      this.$message({
        type: "info",
        message: `已取消${this.pagingname}`,
      });
    },
    // 取消按钮
    resetField(formName) {
      this.$refs[formName].resetFields();
      this.$emit("ResetData", true);
      this.$emit("isDialog", false);
      this.$message({
        type: "info",
        message: `已取消${this.pagingname}`,
      });
    },
    // 确认按钮
    sureEdit(formName) {
      this.ruleForm.indexSetCalcuSql = this.ruleForm.indexSetCalcuSql.trim();
      this.ruleForm.createUser = this.ruleForm.createUser.trim();
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.JudgeConditions) {
            this.UpdateConfiguration(this.ruleForm);
          } else {
            this.NewConfiguration(this.ruleForm);
          }
          this.$emit("ResetData", true);
          this.$emit("isDialog", false);
        } else {
          return false;
        }
      });
    },
    // 指标配置更新
    async UpdateConfiguration(row) {
      let res = await UpdateInterface(row);
      if (res.code == 0) {
        this.$message({
          type: "success",
          message: "编辑成功!",
        });
        this.$emit("ConditionSearchEmpty", true);
      } else {
        this.$message({
          showClose: true,
          message: res.msg,
          type: "error",
        });
      }
    },
    // 指标配置新增
    async NewConfiguration(row) {
      let res = await IndexOfNew(row);
      if (res.code == 0) {
        this.$message({
          type: "success",
          message: "添加成功!",
        });
        this.$emit("ConditionSearchEmpty", true);
      } else {
        this.$message({
          showClose: true,
          message: res.msg,
          type: "error",
        });
      }
    },
  },
};
</script>

<style scoped>
.Popup_inputwidth {
  width: 80%;
}
</style>